<?php

namespace App\Http\Controllers\Admin;

use App\Imports\GameImport;
use App\Models\Category;
use App\Models\Game;
use App\Models\Grade;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class GameController extends Controller
{
    //
    public function index(Request $request){
        $query = $request->only(['name', 'code', 'category_id','grade_id']);

        $where = Game::selectQuery($query);
        $list = Game::where($where)->paginate(config('site.other.paginate'));
        $list = $list->appends($query);

        $categories = Category::all();
        $grades = Grade::all();

        return backendView('game.index', compact('list', 'categories', 'grades', 'query'));
    }

    public function add(Request $request){
        if ($request->isMethod('post')){
            $this->validate($request, [
                'name' => 'required|max:255',
                'code' => 'required|max:255',
                'category_id' => 'required|integer|exists:categories,id',
                'grade_id' => 'required|integer|exists:grades,id',
            ], [], [
                'name' => __("Name"),
                'code' => __("code"),
                'category_id' => __("category"),
                'grade_id' => __("grade"),
            ]);

            $data = $request->only(['name', 'code', 'category_id', 'grade_id']);

            if (Game::create($data)) {
                return redirect()->route('admin.game.index')->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        }else{
            $categories = Category::all();
            $grades = Grade::all();

            return backendView('game.add', compact('categories', 'grades'));
        }
    }

    public function edit($id, Request $request){
        $game = Game::find($id);

        if (empty($game)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod("post")){
            $this->validate($request, [
                'name' => 'required|max:255',
                'code' => 'required|max:255',
                'category_id' => 'required|integer|exists:categories,id',
                'grade_id' => 'required|integer|exists:grades,id',
            ], [], [
                'name' => __("Name"),
                'code' => __("code"),
                'category_id' => __("category"),
                'grade_id' => __("grade"),
            ]);

            $data = $request->only(['name', 'code', 'category_id', 'grade_id']);

            if ($game->update($data)) {
                return redirect()->route('admin.game.index')->with('success', __('Create success'));
            }
        }else{
            $categories = Category::all();
            $grades = Grade::all();

            return backendView('game.edit', compact('game','categories', 'grades'));
        }
    }

    public function delete($id){
        $game = Game::find($id);

        if (empty($game)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($game->delete()){
            return back()->with('success', __('Delete success'));
        }

        return back()->with('error', __('Delete failed'));
    }

    public function view($id){

    }

    public function import(Request $request){
        if ($request->isMethod("post")){
            $validate = [
                'excel' => 'required|file|max:2000',
            ];

            $this->validate($request, $validate, [], [
                'excel' => '数据文件',
            ]);

            $reuqistdata = $request->only(['excel']);
            $file = $reuqistdata['excel'];

            $dd = Excel::import(new GameImport, $file);

            if ($dd){
                return back()->with('success', "导入成功");
            }

            return back()->with('error', "导入失败");
        }else{
            return backendView("game.import");
        }
    }
}
